from PyQt5.QtGui import QPolygonF, QPainter, QColor
from PyQt5.QtCore import pyqtSignal, Qt, QDate, QTime, QRectF, QPoint, QTimer
from PyQt5.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QGridLayout, QApplication, QGroupBox, \
    QLabel, QLineEdit, QComboBox, QPushButton, QTableWidget, QHeaderView, QSizePolicy, QSpacerItem, \
    QCalendarWidget, QAbstractItemView, QTableWidgetItem, QMessageBox
from qdarkstyle import load_stylesheet_pyqt5
from numpy import floor, float64, power, sin, cos, pi, arctan, arcsin, ceil

from Rayleigh_Simulation_Dialog import Button_Simulation
from DoP_Mview import DoP_Model
import threading

class PointerClock(QLabel):
    def __init__(self, flag = None):
        super().__init__()
        self.flag = flag
        self.timer = QTimer()
        self.timer.timeout.connect(self.update)
        if self.flag == None :
            self.timer.start(1000)
        else :
            self.timer.stop()
    def paintEvent(self, event):

        hour_point = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -30)]
        min_point = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -65)]
        secn_point = [QPoint(7, 8), QPoint(-7, 8), QPoint(0, -80)]

        hour_color = QColor(182, 98, 0, 182)
        min_color = QColor(0, 130, 130, 155)
        sec_color = QColor(0, 155, 227, 155)

        min_size = min(self.width(), self.height())

        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)

        painter.translate(self.width() / 2, self.height() / 2)

        painter.scale(int(min_size / 200), int(min_size / 200))

        painter.save()

        for a in range(0, 60):
            if (a % 5) != 0:
                painter.setPen(min_color)
                painter.drawLine(92, 0, 96, 0)
            else:
                painter.setPen(hour_color)
                painter.drawLine(88, 0, 96, 0)
            painter.rotate(360 / 60)
        painter.restore()

        painter.save()
        font = painter.font()
        font.setBold(True)
        painter.setFont(font)
        font_size = font.pointSize()
        painter.setPen(hour_color)
        hour_num = 0
        radius = 100
        for i in range(0, 12):
            hour_num = i + 3
            if hour_num > 12:
                hour_num = hour_num - 12
            x = radius * 0.8 * cos(i * 30 * pi / 180.0) - font_size
            y = radius * 0.8 * sin(i * 30 * pi / 180.0) - font_size / 2.0
            width = font_size * 2
            height = font_size
            painter.drawText(QRectF(x, y, width, height), Qt.AlignCenter, str(hour_num))
        painter.restore()

        time = QTime.currentTime()

        painter.save()
        painter.setPen(Qt.NoPen)
        painter.setBrush(hour_color)
        painter.rotate(30 * (time.hour() + time.minute() / 60))
        painter.drawConvexPolygon(QPolygonF(hour_point))
        painter.restore()

        painter.save()
        painter.setPen(Qt.NoPen)
        painter.setBrush(min_color)
        painter.rotate(6 * (time.minute() + time.second() / 60))
        painter.drawConvexPolygon(QPolygonF(min_point))
        painter.restore()

        painter.save()
        painter.setPen(Qt.NoPen)
        painter.setBrush(sec_color)
        painter.rotate(6 * time.second())
        painter.drawConvexPolygon(QPolygonF(secn_point))
        painter.restore()
    def endTimer(self):
        self.timer.stop()

class Main_Window(QWidget):
    IsCalculat_signal = pyqtSignal()
    def __init__(self, parent=None):
        super(Main_Window, self).__init__(parent)
        self.setWindowTitle("太阳位置计算界面")
        self.setWindowModality(Qt.WindowModal)
        self.Declination_Angle = None
        self.Hour = None
        self.Minutes = None
        self.Second = None
        self.TimeZone_Number = None
        self.Sim_widget = None
        self.Scala_DoP = None
        lineEditQSS = """
                            QLineEdit {
                                    border-radius:8px;
                                    border: 2px solid rgb(118, 121, 124);
                                    font-family:Times New Roman;
                                    font-size:10pt;
                                    }

                            QLineEdit:hover{
                                        background-color:#426a6a;
                                        color:white;
                                        border: 2px solid rgb(61, 174, 233);
                                        }
                            """
        gropBoxQSS = """
                            border-radius:5px;
                            font-size:10pt;
                            font-family:"Times New Roman" , "Microsoft YaHei";
                            border-width: 2px;
                            """
        labelQSS = """
                            QLabel {
                                    border-radius:10px;
                                    border: 2px solid rgb(49, 54, 59);
                                    font-size:10pt;
                                    font-family:"Times New Roman" , "Microsoft YaHei";}
                            QLabel:hover{
                                    background-color:rgb(24, 70, 93);
                                    color:white;
                                    border: 1px solid rgb(61, 174, 233);
                                    }
                            """
        label1QSS = """
                        QLabel {
                                font-family:"Times New Roman" , "Microsoft YaHei";
                                font-size:10pt;
                                }             
                """
        comBoxQSS = """
                                    QComboBox {
                                                font: 10pt "Times New Roman";
                                                min-width: 35px;
                                                min-height:20px; 
                                                border-radius: 15px;
                                                border:2px solid rgb(118, 121, 124);
                                                }
                                    QComboBox:hover {	
                                                /* 鼠标悬浮在QComboBox上时的状态 */
                                                background-color: rgb(24, 70, 93);
                                                border:2px solid rgb(61, 174, 233);
                                                color: white;}
                                    /* 下拉列表里的颜色 */
                                    QComboBox QAbstractItemView {
                                        border: 2px solid rgb(61, 174, 233);/*边框宽度、线形、颜色*/
                                        border-radius: 15px;/*圆角*/
                                        min-width: 2em;   /*# 组合框的最小宽度*/
                                        padding: 1px 2px 1px 2px;  /*针对于组合框中的文本内容*/
                                        selection-color: rgb(61, 174, 233);
                                        selection-background-color: rgb(49, 54, 59);
                                        background-color: rgb(66, 106, 106);/*背景颜色*/
                                    }
                                """
        buttonQSS = """
                                 QPushButton {font-size: 18pt;
                                    background-color:rgb(35, 38, 41);
                                    border: 2px solid rgb(60, 63, 65);
                                    border-radius: 30px;} 

                                QPushButton:hover {	/* 鼠标悬浮在QPushButton上时的状态 */
                                    background-color: rgb(66, 106, 106);
                                    border-color:rgb(61, 174, 233);
                                    }
                                QPushButton:pressed { /* 鼠标按压在QPushButton上时的状态 */
                        	        background-color: #35071f;
                        	        border-color:rgb(61, 174, 233);}
                                """
        tabelQSS = """
                                QTableWidget
                                        {
                                        font-family:"Times New Roman" , "Microsoft YaHei";
                                        font-size:10pt;
                                        border: 2px solid rgb(118, 121, 124);
                                        border-radius:10px;
                                        selection-background-color:black;}
                                QTableWidget::item
                                        { / ** 每个单元格设置 * /
                                            background-color:white;
                                            border: none;
                                            background - color: rgb(220, 220, 220); / *交替行，第二行 * /
                                            selection - background - color: white;
                                            selection - color: black;
                                            font - family: Consolas;
                                            font - size: 5px;
                                            text - align: center;
                                            height: 50px;}
                        """

        self.All_VLayout = QVBoxLayout()

        self.Up_GroupBox = QGroupBox("时间显示窗口")
        self.Up_GroupBox.setStyleSheet(gropBoxQSS)
        self.All_VLayout.addWidget(self.Up_GroupBox)

        self.TimeGLayout = QGridLayout()
        self.Up_GroupBox.setLayout(self.TimeGLayout)

        self.TimeClock = PointerClock()
        self.TimeClock.setMinimumWidth(530)

        self.cal = QCalendarWidget(self)
        self.cal.setStyleSheet("""
                                        border-radius:5px;
                                        border: 1px solid rgb(118, 121, 124);
                                        font-size:10pt;
                                        font-family:"Times New Roman" , "Microsoft YaHei";
                                        """)
        self.cal.setMinimumDate(QDate(1956, 1, 1))
        self.cal.setMaximumDate(QDate(3000, 1, 1))
        self.cal.setGridVisible(True)
        self.cal.setVerticalHeaderFormat(False)

        self.TimeGLayout.addWidget(self.TimeClock, 0, 0, 4, 4)
        self.TimeGLayout.addWidget(self.cal, 0, 4, 4, 5)

        self.MD_GroupBox = QGroupBox("数据输入/结果")

        self.MD_GroupBox.setStyleSheet(gropBoxQSS)
        self.All_VLayout.addWidget(self.MD_GroupBox)

        self.MD_HLayout = QHBoxLayout()

        self.MD_GroupBox.setLayout(self.MD_HLayout)

        self.Left_VLayout = QVBoxLayout()

        self.Time_HLayout = QHBoxLayout()

        self.gongyuan = QLabel("时间:")
        self.gongyuan.setStyleSheet(label1QSS)

        self.Yearwt_LEdit = QLineEdit()
        self.Yearwt_LEdit.setAlignment(Qt.AlignCenter)
        self.Yearwt_LEdit.setStyleSheet(lineEditQSS)

        self.Yearview_Label = QLabel("年")
        self.Yearview_Label.setStyleSheet(label1QSS)

        self.Month_LEdit = QLineEdit()
        self.Month_LEdit.setAlignment(Qt.AlignCenter)
        self.Month_LEdit.setStyleSheet(lineEditQSS)

        self.Month_Label = QLabel("月")
        self.Month_Label.setStyleSheet(label1QSS)

        self.Day_LEdit = QLineEdit()
        self.Day_LEdit.setAlignment(Qt.AlignCenter)
        self.Day_LEdit.setStyleSheet(lineEditQSS)

        self.Day_Label = QLabel("日")
        self.Day_Label.setStyleSheet(label1QSS)

        Hour_list = []
        for i in range(0, 24):
            x = str(i)
            Hour_list.append(x)
        self.HourComboBox = QComboBox()
        self.HourComboBox.addItems(Hour_list)
        self.HourComboBox.setStyleSheet(comBoxQSS)

        self.Hour_Label = QLabel("时")
        self.Hour_Label.setStyleSheet(label1QSS)

        Minutes_list = []
        for i in range(0, 60):
            s = "%02d" % i
            x = str(s)
            Minutes_list.append(x)
        self.MinutesComboBox = QComboBox()
        self.MinutesComboBox.addItems(Minutes_list)
        self.MinutesComboBox.setStyleSheet(comBoxQSS)

        self.Minetes_Label = QLabel("分")
        self.Minetes_Label.setStyleSheet(label1QSS)

        Second_list = []
        for i in range(0, 60):
            s = "%02d" % i
            x = str(s)
            Second_list.append(x)
        self.SecondComboBox = QComboBox()
        self.SecondComboBox.addItems(Second_list)
        self.SecondComboBox.setStyleSheet(comBoxQSS)

        self.Second_Label = QLabel("秒")
        self.Second_Label.setStyleSheet(label1QSS)

        self.Loc_GLayout = QGridLayout()

        self.Long_HLayout = QHBoxLayout()

        self.Long_agle = QLabel("经度:")
        self.Long_agle.setStyleSheet(label1QSS)

        EastWest_List = ["东+", "西-"]
        self.EastWest_ComboBox = QComboBox()
        self.EastWest_ComboBox.setStyleSheet(comBoxQSS)

        self.EastWest_ComboBox.addItems(EastWest_List)

        self.LongF_LEdit = QLineEdit()
        self.LongF_LEdit.setAlignment(Qt.AlignCenter)
        self.LongF_LEdit.setStyleSheet(lineEditQSS)

        self.LongFS_Label = QLabel("°")
        self.LongFS_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LongS_LEdit = QLineEdit()
        self.LongS_LEdit.setAlignment(Qt.AlignCenter)
        self.LongS_LEdit.setStyleSheet(lineEditQSS)

        self.LongST_Label = QLabel("'")
        self.LongST_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LongT_LEdit = QLineEdit()
        self.LongT_LEdit.setAlignment(Qt.AlignCenter)
        self.LongT_LEdit.setStyleSheet(lineEditQSS)

        self.LongTF_Label = QLabel("''")
        self.LongTF_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LongTR_Label = QLabel(">>")
        self.LongTR_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LongRead_LEdit = QLineEdit()
        self.LongRead_LEdit.setAlignment(Qt.AlignCenter)
        self.LongRead_LEdit.setStyleSheet(lineEditQSS)

        self.Longdanwei_Label = QLabel("°")
        self.Longdanwei_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.Lat_HLayout = QHBoxLayout()
        self.Lat_agle = QLabel("纬度:")
        self.Lat_agle.setStyleSheet(label1QSS)

        SouthNorth_List = ["北+", "南-"]
        self.SouthNorth_ComboBox = QComboBox()
        self.SouthNorth_ComboBox.setStyleSheet(comBoxQSS)
        self.SouthNorth_ComboBox.addItems(SouthNorth_List)

        self.LatF_LEdit = QLineEdit()
        self.LatF_LEdit.setAlignment(Qt.AlignCenter)
        self.LatF_LEdit.setStyleSheet(lineEditQSS)

        self.LatFS_Label = QLabel("°")
        self.LatFS_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LatS_LEdit = QLineEdit()
        self.LatS_LEdit.setAlignment(Qt.AlignCenter)
        self.LatS_LEdit.setStyleSheet(lineEditQSS)

        self.LatST_Label = QLabel("'")
        self.LatST_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LatT_LEdit = QLineEdit()
        self.LatT_LEdit.setAlignment(Qt.AlignCenter)
        self.LatT_LEdit.setStyleSheet(lineEditQSS)

        self.LatTF_Label = QLabel("''")
        self.LatTF_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LatTR_Label = QLabel(">>")
        self.LatTR_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.LatRead_LEdit = QLineEdit()
        self.LatRead_LEdit.setAlignment(Qt.AlignCenter)
        self.LatRead_LEdit.setStyleSheet(lineEditQSS)

        self.Latdanwei_Label = QLabel("°")
        self.Latdanwei_Label.setStyleSheet("""font: 10pt "Times New Roman";""")

        self.TimeZoonLabel = QLabel("时区:")
        self.TimeZoonLabel.setStyleSheet(label1QSS)
        self.TimeZoonLabel.setAlignment(Qt.AlignCenter)

        self.TimeZoomComboBox = QComboBox()
        self.TimeZoomComboBox.setFixedWidth(120)
        Number = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二"]
        Timezoon_list = []
        for i in range(0, 12):
            s = "东" + Number[i] + "区"
            x = str(s)
            Timezoon_list.append(x)
        Timezoon_list.append(str("零时区"))
        for i in range(0, 12):
            s = "西" + Number[i] + "区"
            x = str(s)
            Timezoon_list.append(x)
        self.TimeZoomComboBox.addItems(Timezoon_list)
        self.TimeZoomComboBox.setCurrentIndex(12)
        self.TimeZoomComboBox.setStyleSheet(comBoxQSS)
        self.TimeZoomLabel = QLabel("*请选择时区，不同的时区计算的太阳位置不同")

        self.PthHigh = QLabel("海拔:")
        self.PthHigh.setStyleSheet(label1QSS)
        self.PthHigh.setAlignment(Qt.AlignCenter)

        self.PthHigh_LinEdit = QLineEdit()
        self.PthHigh_LinEdit.setAlignment(Qt.AlignCenter)
        self.PthHigh_LinEdit.setStyleSheet(lineEditQSS)

        self.MButton_VLayout = QVBoxLayout()

        self.Calculation_Button = QPushButton("计算")
        self.Calculation_Button.setMinimumHeight(100)
        self.Calculation_Button.setMinimumWidth(100)
        self.Calculation_Button.setStyleSheet(buttonQSS)

        self.Init_Button = QPushButton("初始化")
        self.Init_Button.setMinimumHeight(100)
        self.Init_Button.setMinimumWidth(100)
        self.Init_Button.setStyleSheet(buttonQSS)

        self.Result_GLayout = QGridLayout()

        self.Time_AgleRD = QLineEdit("太阳时角:")
        self.Time_AgleRD.setReadOnly(True)
        self.Time_AgleRD.setAlignment(Qt.AlignCenter)
        self.Time_AgleRD.setStyleSheet(label1QSS)
        self.Time_AgleRD.setFixedWidth(100)

        self.Time_AgleWT = QLineEdit()
        self.Time_AgleWT.setReadOnly(True)
        self.Time_AgleWT.setAlignment(Qt.AlignCenter)
        self.Time_AgleWT.setStyleSheet(lineEditQSS)

        self.Time_AgleWT.setFixedWidth(113)

        self.ChiWei_AgleRD = QLineEdit("太阳赤纬角:")
        self.ChiWei_AgleRD.setReadOnly(True)
        self.ChiWei_AgleRD.setAlignment(Qt.AlignCenter)
        self.ChiWei_AgleRD.setStyleSheet(label1QSS)
        self.ChiWei_AgleRD.setFixedWidth(100)

        self.ChiWei_AgleWT = QLineEdit()
        self.ChiWei_AgleWT.setReadOnly(True)
        self.ChiWei_AgleWT.setAlignment(Qt.AlignCenter)
        self.ChiWei_AgleWT.setStyleSheet(lineEditQSS)
        self.ChiWei_AgleWT.setFixedWidth(113)

        self.SunHight_AgleRD = QLineEdit("太阳高度角:")
        self.SunHight_AgleRD.setReadOnly(True)
        self.SunHight_AgleRD.setAlignment(Qt.AlignCenter)
        self.SunHight_AgleRD.setStyleSheet(label1QSS)
        self.SunHight_AgleRD.setFixedWidth(100)

        self.SunHight_AgleWT = QLineEdit()
        self.SunHight_AgleWT.setReadOnly(True)
        self.SunHight_AgleWT.setAlignment(Qt.AlignCenter)
        self.SunHight_AgleWT.setStyleSheet(lineEditQSS)
        self.SunHight_AgleWT.setFixedWidth(113)

        self.SunAzim_AgleRD = QLineEdit("太阳方位角:")
        self.SunAzim_AgleRD.setReadOnly(True)
        self.SunAzim_AgleRD.setAlignment(Qt.AlignCenter)
        self.SunAzim_AgleRD.setStyleSheet(lineEditQSS)
        self.SunAzim_AgleRD.setFixedWidth(100)

        self.SunAzim_AgleWT = QLineEdit()
        self.SunAzim_AgleWT.setReadOnly(True)
        self.SunAzim_AgleWT.setAlignment(Qt.AlignCenter)
        self.SunAzim_AgleWT.setStyleSheet(lineEditQSS)
        self.SunAzim_AgleWT.setFixedWidth(113)

        self.TabelView = QTableWidget()
        self.TabelView.horizontalHeader().setStretchLastSection(False)
        self.TabelView.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
        self.TabelView.setEditTriggers((QAbstractItemView.NoEditTriggers))
        self.TabelView.verticalHeader().setVisible(False)
        self.TabelView.setColumnCount(5)
        self.TabelView.setRowCount(1)
        self.TabelView.setRowHeight(80, 30)
        self.TabelView.setMinimumWidth(460)
        self.TabelView.setStyleSheet(tabelQSS)

        self.TabelView.setHorizontalHeaderLabels([" 项目", "视赤经", "视赤纬", " 视角", " 时差"])
        item = QTableWidgetItem("太阳")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 0, item)

        self.Below_HLayout = QHBoxLayout()
        self.BelowL_Spacer = QSpacerItem(400, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)

        self.Simulation_Button = QPushButton("仿真")
        self.Simulation_Button.setMinimumSize(100, 100)
        self.Simulation_Button.setStyleSheet(buttonQSS)

        self.BelowM_Spacer = QSpacerItem(50, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)

        self.Clear_Button = QPushButton("清空")
        self.Clear_Button.setMinimumSize(100, 100)
        self.Clear_Button.setStyleSheet(buttonQSS)

        self.BelowR_Spacer = QSpacerItem(400, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)

        self.TimeUP_Spacer = QSpacerItem(10, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)
        self.Left_VLayout.addSpacerItem(self.TimeUP_Spacer)

        self.Time_HLayout.addWidget(self.gongyuan)
        self.Time_HLayout.addWidget(self.Yearwt_LEdit)
        self.Time_HLayout.addWidget(self.Yearview_Label)
        self.Time_HLayout.addWidget(self.Month_LEdit)
        self.Time_HLayout.addWidget(self.Month_Label)
        self.Time_HLayout.addWidget(self.Day_LEdit)
        self.Time_HLayout.addWidget(self.Day_Label)
        self.Time_HLayout.addWidget(self.HourComboBox)
        self.Time_HLayout.addWidget(self.Hour_Label)
        self.Time_HLayout.addWidget(self.MinutesComboBox)
        self.Time_HLayout.addWidget(self.Minetes_Label)
        self.Time_HLayout.addWidget(self.SecondComboBox)
        self.Time_HLayout.addWidget(self.Second_Label)
        self.Left_VLayout.addLayout(self.Time_HLayout)

        self.Long_HLayout.addWidget(self.Long_agle)
        self.Long_HLayout.addWidget(self.EastWest_ComboBox)
        self.Long_HLayout.addWidget(self.LongF_LEdit)
        self.Long_HLayout.addWidget(self.LongFS_Label)
        self.Long_HLayout.addWidget(self.LongS_LEdit)
        self.Long_HLayout.addWidget(self.LongST_Label)
        self.Long_HLayout.addWidget(self.LongT_LEdit)
        self.Long_HLayout.addWidget(self.LongTF_Label)
        self.Long_HLayout.addWidget(self.LongTR_Label)
        self.Long_HLayout.addWidget(self.LongRead_LEdit)
        self.Long_HLayout.addWidget(self.Longdanwei_Label)
        self.Left_VLayout.addLayout(self.Long_HLayout)

        self.Lat_HLayout.addWidget(self.Lat_agle)
        self.Lat_HLayout.addWidget(self.SouthNorth_ComboBox)
        self.Lat_HLayout.addWidget(self.LatF_LEdit)
        self.Lat_HLayout.addWidget(self.LatFS_Label)
        self.Lat_HLayout.addWidget(self.LatS_LEdit)
        self.Lat_HLayout.addWidget(self.LatST_Label)
        self.Lat_HLayout.addWidget(self.LatT_LEdit)
        self.Lat_HLayout.addWidget(self.LatTF_Label)
        self.Lat_HLayout.addWidget(self.LatTR_Label)
        self.Lat_HLayout.addWidget(self.LatRead_LEdit)
        self.Lat_HLayout.addWidget(self.Latdanwei_Label)
        self.Left_VLayout.addLayout(self.Lat_HLayout)

        self.Loc_GLayout.addWidget(self.PthHigh, 0, 0, 1, 1)
        self.Loc_GLayout.addWidget(self.PthHigh_LinEdit, 0, 1, 1, 2)
        self.Loc_GLayout.addWidget(self.TimeZoonLabel, 1, 0, 1, 1)
        self.Loc_GLayout.addWidget(self.TimeZoomComboBox, 1, 1, 1, 2)
        self.Loc_GLayout.addWidget(self.TimeZoomLabel, 1, 3, 1, 9)

        self.Left_VLayout.addLayout(self.Loc_GLayout)

        self.LocDN_Spacer = QSpacerItem(10, 10, QSizePolicy.Fixed, QSizePolicy.Expanding)
        self.Left_VLayout.addSpacerItem(self.LocDN_Spacer)

        self.MD_HLayout.addLayout(self.Left_VLayout)

        self.MButton_VLayout.addWidget(self.Calculation_Button)
        self.MButton_VLayout.addWidget(self.Init_Button)
        self.MD_HLayout.addLayout(self.MButton_VLayout)

        self.Result_GLayout.addWidget(self.TabelView, 0, 0, 1, 4)

        self.Result_GLayout.addWidget(self.Time_AgleRD, 1, 1, 1, 1)
        self.Result_GLayout.addWidget(self.Time_AgleWT, 1, 2, 1, 2)
        self.Result_GLayout.addWidget(self.ChiWei_AgleRD, 2, 1, 1, 1)
        self.Result_GLayout.addWidget(self.ChiWei_AgleWT, 2, 2, 1, 2)
        self.Result_GLayout.addWidget(self.SunHight_AgleRD, 3, 1, 1, 1)
        self.Result_GLayout.addWidget(self.SunHight_AgleWT, 3, 2, 1, 2)
        self.Result_GLayout.addWidget(self.SunAzim_AgleRD, 4, 1, 1, 1)
        self.Result_GLayout.addWidget(self.SunAzim_AgleWT, 4, 2, 1, 2)

        self.MD_HLayout.addLayout(self.Result_GLayout)

        self.Below_HLayout.addSpacerItem(self.BelowL_Spacer)
        self.Below_HLayout.addWidget(self.Simulation_Button)
        self.Below_HLayout.addSpacerItem(self.BelowM_Spacer)
        self.Below_HLayout.addWidget(self.Clear_Button)
        self.Below_HLayout.addSpacerItem(self.BelowR_Spacer)

        self.All_VLayout.addLayout(self.Below_HLayout)

        self.setLayout(self.All_VLayout)
        self.setup_signal()
    # all signal
    def setup_signal(self):
        self.cal.clicked[QDate].connect(self.View_YearMonDay)   # 时钟信号

        self.HourComboBox.activated.connect(self.Selection_change)       # Hours
        self.MinutesComboBox.activated.connect(self.Selection_change)    # Minutes
        self.SecondComboBox.activated.connect(self.Selection_change)     # Second

        self.LongRead_LEdit.returnPressed.connect(self.Long_setTxt_change)   # Long
        self.LatRead_LEdit.returnPressed.connect(self.Lat_setTxt_change)     # Lat

        self.Calculation_Button.clicked.connect(self.Calculate)             # Calculation Azim and High

        self.Init_Button.clicked.connect(self.Initialize)                   # Init

        self.Clear_Button.clicked.connect(self.ClearKeyValue)               # Clear all

        self.Simulation_Button.clicked.connect(self.AoPwDoP_view)           # Matplotlib draw Aop&Dop jet and Scala DoP
    # 年月日
    def View_YearMonDay(self):
        date = self.cal.selectedDate()
        year = date.toString("yyyy")
        month = date.toString("M")
        day = date.toString("d")
        self.Yearwt_LEdit.setText(year)
        self.Month_LEdit.setText(month)
        self.Day_LEdit.setText(day)
    # 时分秒
    def Selection_change(self):
        self.Hour = self.HourComboBox.currentText()
        self.Minutes = self.MinutesComboBox.currentText()
        self.Second = self.SecondComboBox.currentText()
    # 经度
    def Long_setTxt_change(self):
        Long_Angle = self.EastWest_ComboBox.currentText()
        Long_Angle_Symbol = Long_Angle[1]
        try:
            if self.LongF_LEdit.text() == '':
                print(QMessageBox.warning(self, '警告', '请输入经度', QMessageBox.Yes, QMessageBox.Yes))
            Long_top2 = int(self.LongF_LEdit.text())
            if self.LongS_LEdit.text() != '':
                Long_between2 = int(self.LongS_LEdit.text())
            else:
                Long_between2 = 0
            if self.LongT_LEdit.text() != '':
                Long_after2 = int(self.LongT_LEdit.text())
            else:
                Long_after2 = 0
            Long = Long_top2 + Long_between2 / 60 + Long_after2 / 3600
            Long = format(Long, '.5f')
            Long_All = str(Long_Angle_Symbol) + str(Long)
            self.LongRead_LEdit.setText(Long_All)
        except ValueError as V:
            print(V)
    # 纬度
    def Lat_setTxt_change(self):
        Lat_Angle = self.SouthNorth_ComboBox.currentText()
        Lat_Angle_Symbol = Lat_Angle[1]
        try:
            if self.LatF_LEdit.text() == '':
                print(QMessageBox.warning(self, '警告', '请输入纬度', QMessageBox.Yes, QMessageBox.Yes))
            Lat_top2 = int(self.LatF_LEdit.text())
            if self.LatS_LEdit.text() != '':
                Lat_between2 = int(self.LatS_LEdit.text())
            else:
                Lat_between2 = 0
            if self.LatT_LEdit.text() != '' :
                Lat_after2 = int(self.LatT_LEdit.text())
            else:
                Lat_after2 = 0
            Lat = Lat_top2 + Lat_between2 / 60 + Lat_after2 / 3600
            Lat = format(Lat, '.5f')
            Lat_All = str(Lat_Angle_Symbol) + str(Lat)
            self.LatRead_LEdit.setText(Lat_All)
        except ValueError as V:
            print(V)
    # 计算高度角方位角
    def Calculate(self):
        try:
            index = self.TimeZoomComboBox.currentIndex()
            list_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12]
            self.TimeZone_Number = float64(list_number[index])

            timezone = self.TimeZone_Number
            year = int(self.Yearwt_LEdit.text())
            month = int(self.Month_LEdit.text())
            day = int(self.Day_LEdit.text())
            long = float(self.LongRead_LEdit.text())
            lat = float(self.LatRead_LEdit.text())
            hours = float(self.HourComboBox.currentText())
            minutes = float(self.MinutesComboBox.currentText())
            seconds = float(self.SecondComboBox.currentText())

            self.TabelView.clearContents()
            item = QTableWidgetItem("太阳")
            item.setTextAlignment(Qt.AlignCenter)
            self.TabelView.setItem(0, 0, item)
            GMT = hours + minutes / 60 + seconds / 3600
            day_hms = GMT / 24
            JD_2 = 0
            if 0 < month <= 2:
                A = int((year - 1) / 100)
                B = 2 - A + int(A / 4)
                JD_2 = int(365.25 * (year - 1 + 4716)) + int(
                    30.6001 * (month + 12 + 1)) + day + day_hms + B - 1524.5
            else:
                A = int(year / 100)
                B = 2 - A + int(A / 4)
                JD_2 = int(365.25 * (year + 4716)) + int(30.6001 * (month + 1)) + day + day_hms + B - 1524.5

            T = (JD_2 - 2451545.0) / 36525
            Om = 125.04452 - 1934.136261 * T + 0.0020708 * T ** 2 + T ** 3 / 450000
            Om = Om - (Om // 360) * 360

            L = 280.4665 + 36000.7698 * T
            L_pie = 218.3165 + 481267.8813 * T

            Date_phi = -17.20 * sin(Om * pi / 180) - 1.32 * sin(2 * L * pi / 180) - \
                       0.23 * sin(2 * L_pie * pi / 180) + 0.21 * sin(2 * Om * pi / 180)

            theta0 = (23 + 26 / 60 + 21.448 / 3600) - (46.8150 / 3600) * T - \
                     (0.00059 / 3600) * power(T, 2) + \
                     (0.001813 / 3600) * power(T, 3)

            L_0 = 280.46645 + 36000.76983 * T + 0.0003032 * power(T, 2)
            L_0 = L_0 - (L_0 // 360) * 360

            M_sun = 357.52910 + 35999.05030 * T - 0.0001559 * power(T, 2) - 0.00000048 * power(T, 3)
            M_sun = M_sun - (M_sun // 360) * 360

            e = 0.016708617 - 0.000042037 * T - 0.0000001236 * power(T, 2)

            C = +(1.914600 - 0.004817 - 0.000014 * power(T, 3)) * sin(M_sun * pi / 180) + \
                (0.019993 - 0.000101 * T) * sin(2 * M_sun * pi / 180) + \
                0.000290 * sin(3 * M_sun * pi / 180)

            Sun_truth_Long = L_0 + C

            v = M_sun + C

            R = 1.000001018 * (1 - power(e, 2)) / (1 + e * cos(v * pi / 180))

            sun_Om = 125.04 - 1934.136 * T

            Lang_da = Sun_truth_Long - 0.00569 - 0.00478 * sin(sun_Om * pi / 180)

            theta2 = theta0 + 0.00256 * cos(sun_Om * pi / 180)
            tan_alpha = cos(theta2 * pi / 180) * sin(Lang_da * pi / 180) / cos(Lang_da * pi / 180)
            alpha_fu = -180 + arctan(tan_alpha) * 180 / pi
            alpha_fu_h_x = alpha_fu / 360 * 24
            alpha_fu_h = ceil(alpha_fu / 360 * 24)
            alpha_fu_m_x = (alpha_fu_h_x - alpha_fu_h) * 60
            alpha_fu_m = ceil(alpha_fu_m_x)
            alpha_fu_s = (alpha_fu_m_x - alpha_fu_m) * 60

            alpha_zheng = 180 + arctan(tan_alpha) * 180 / pi
            alpha_zheng_h_x = alpha_zheng / 360 * 24
            alpha_zheng_h = floor(alpha_zheng / 360 * 24)
            alpha_zheng_m_x = (alpha_zheng_h_x - alpha_zheng_h) * 60
            alpha_zheng_m = floor(alpha_zheng_m_x)
            alpha_zheng_s = (alpha_zheng_m_x - alpha_zheng_m) * 60

            sin_date = sin(theta2 * pi / 180) * sin(Lang_da * pi / 180)
            sun_date = arcsin(sin_date) * 180 / pi
            self.Declination_Angle = sun_date
            if sun_date < 0:
                sun_date_du = ceil(sun_date)
                sun_date_fen = (sun_date_du - sun_date) * 60
            else:
                sun_date_du = floor(sun_date)
                sun_date_fen = (sun_date - sun_date_du) * 60
            sun_date_miao = (sun_date_fen - floor(sun_date_fen)) * 60

            month = int(self.Month_LEdit.text())
            if 1 <= month <= 3:
                alpha_zheng_h += 12
                if alpha_zheng_h == 24:
                    alpha_zheng_h = str("00")
            elif 4 <= month <= 6:
                alpha_zheng_h -= 12
            else:
                alpha_zheng_h = alpha_zheng_h

            str_alpha_h = str(alpha_zheng_h)
            str_alpha_m = str(alpha_zheng_m)
            str_alpha_s = str(round(alpha_zheng_s))
            str2alpha = str_alpha_h + 'h' + str_alpha_m + 'm' + str_alpha_s + 's'

            item = QTableWidgetItem(str2alpha)
            item.setTextAlignment(Qt.AlignCenter)
            self.TabelView.setItem(0, 1, item)

            str_sundate_du = str(sun_date_du)
            str_sundate_m = str(floor(sun_date_fen))
            str_sundate_miao = str(round(sun_date_miao))
            str2sundate = str_sundate_du + '°' + str_sundate_m + "'" + str_sundate_miao + "''"

            item = QTableWidgetItem(str2sundate)
            item.setTextAlignment(Qt.AlignCenter)
            self.TabelView.setItem(0, 2, item)

            sun_d = 1.392 * power(10, 6)
            tan_r = sun_d / 2 / (R * 1.496 * power(10, 8))
            artan_r = arctan(tan_r) * 180 / pi * 2
            sight_r2hms = artan_r / 360 * 24
            sight_r2h = floor(sight_r2hms)
            sight_r2m = (sight_r2hms - sight_r2h) * 60
            sight_r2s = (sight_r2m - floor(sight_r2m)) * 60
            sight_r2s = format(sight_r2s, '.6f')
            str_sundate_h = str(sight_r2h)
            str_sundate_m = str(floor(sight_r2m))
            str_sundate_s = str(sight_r2s)
            if str_sundate_h == '0.0':
                if str_sundate_m == '0.0':
                    str2hms = str_sundate_s + 's'
                else:
                    str2hms = str_sundate_m + 'm' + str_sundate_s + 's'
            else:
                str2hms = str_sundate_h + 'h' + str_sundate_m + 'm' + str_sundate_s + 's'
            item_r = QTableWidgetItem(str2hms)
            item_r.setTextAlignment(Qt.AlignCenter)
            self.TabelView.setItem(0, 3, item_r)

            tao1 = T / 10

            E_L0 = 280.4664567 + 360007.6982779 * tao1 + 0.03032028 * power(tao1, 2) + power(tao1, 3) / 49931 - \
                   power(tao1, 4) / 15299 - power(tao1, 5) / 1988000
            E_L0_360 = E_L0 - floor(E_L0 / 360) * 360

            E = E_L0_360 - 0.0057183 - alpha_zheng + Date_phi * cos(theta0 * pi / 180) / 3600

            E_hms = E / 360 * 24
            E_h = floor(E_hms)
            E_m = (E_hms - E_h) * 60
            E_s = (E_m - floor(E_m)) * 60

            str_Eh = str(E_h)
            str_Em = str(floor(E_m))
            str_Es = str(round(E_s))
            if E_h != 0:
                str2E = str_Eh + 'h' + str_Em + 'm' + str_Es + 's'
            else:
                str2E = str_Em + 'm' + str_Es + 's'

            item = QTableWidgetItem(str2E)
            item.setTextAlignment(Qt.AlignCenter)
            self.TabelView.setItem(0, 4, item)

            from Calculate_HighAzim2angle import Solar_angle

            Solar_angle = Solar_angle(timezone, year, month, day, hours, minutes, seconds, long, lat,
                                    Declination=self.Declination_Angle)
            time_angle, Declination_angle, son_alpha, Solar_Azimuth = Solar_angle.Calcu()
            time_angle = format(time_angle, '.6f')
            declination_angle = format(Declination_angle, '.6f')
            solar_high_angle = format(son_alpha, '.6f')
            solar_azimuth_angle = format(Solar_Azimuth, '.6f')

            self.Time_AgleWT.setText(str(time_angle))
            self.ChiWei_AgleWT.setText(str(declination_angle))
            self.SunHight_AgleWT.setText(str(solar_high_angle))
            self.SunAzim_AgleWT.setText(str(solar_azimuth_angle))

        except ValueError as v:
            if self.Yearwt_LEdit.text() == '' or self.Month_LEdit.text() == '' or self.Day_LEdit.text() == '':
                print(QMessageBox.warning(self, '警告', '请输入具体日期', QMessageBox.Yes, QMessageBox.Yes))
            elif self.LongRead_LEdit.text() == "" or self.LongF_LEdit.text() == '':
                print(QMessageBox.warning(self, '警告', '请输入完整的经度', QMessageBox.Yes, QMessageBox.Yes))
            elif self.LatRead_LEdit.text() == "" or self.LatF_LEdit.text() == '':
                print(QMessageBox.warning(self, '警告', '请输入完整的纬度', QMessageBox.Yes, QMessageBox.Yes))
        except AttributeError as a:
            print(QMessageBox.warning(self, '警告', '请先点击[确定]按钮查看太阳"视"信息', QMessageBox.Yes, QMessageBox.Yes))
            print(a)
    # 可视化AoP&DoP 和 Scala DoP
    def AoPwDoP_view(self):
        try:
            long = float(self.LongRead_LEdit.text())
            lat = float(self.LatRead_LEdit.text())

            time_zone = int(self.TimeZone_Number)

            year = int(self.Yearwt_LEdit.text())
            month = int(self.Month_LEdit.text())
            day = int(self.Day_LEdit.text())
            hours = int(self.HourComboBox.currentText())
            minutes = int(self.MinutesComboBox.currentText())
            seconds = int(self.SecondComboBox.currentText())

            self.Sim_widget = Button_Simulation(time_zone, lat, long, year, month, day, hours, minutes, seconds,
                                            Declination=self.Declination_Angle)

            self.Scala_DoP = DoP_Model(time_zone, lat, long, year, month, day, hours, minutes, seconds,
                                       Declination=self.Declination_Angle)
            self.Sim_widget.show()
            self.Scala_DoP.show()

        except AttributeError and ValueError as A:
            print(QMessageBox.warning(self, '警告', '请完善信息和数据后进行仿真！', QMessageBox.Yes, QMessageBox.Yes))
    # 初始化, 显示设定的时间、地点、时角、高度角、方位角
    def Initialize(self):
        date = QDate(2006, 6, 15)
        self.cal.setSelectedDate(date)

        self.Yearwt_LEdit.setText("2006")
        self.Month_LEdit.setText("6")
        self.Day_LEdit.setText("15")

        self.HourComboBox.setCurrentText("9")
        self.MinutesComboBox.setCurrentText("53")
        self.SecondComboBox.setCurrentText("49")
        self.PthHigh_LinEdit.setText("336")
        item = QTableWidgetItem("5.0h34.0m36s")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 1, item)

        item = QTableWidgetItem("23.0°18.0'45''")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 2, item)

        item_r = QTableWidgetItem("2.0m5.961890s")
        item_r.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 3, item_r)

        item = QTableWidgetItem("-13.0h59.0m35s")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 4, item)

        self.LongF_LEdit.setText("120")
        self.LongS_LEdit.setText("38")
        self.LongT_LEdit.setText("00")
        self.LongRead_LEdit.setText("+120.6333")

        self.LatF_LEdit.setText("39")
        self.LatS_LEdit.setText("54")
        self.LatT_LEdit.setText("00")
        self.LatRead_LEdit.setText("+39.9000")

        self.TimeZoomComboBox.setCurrentText("东八区")
        index = self.TimeZoomComboBox.currentIndex()
        list_number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12]
        self.TimeZone_Number = float64(list_number[index])
        print(self.TimeZone_Number)
        from Calculate_HighAzim2angle import Solar_angle

        Solar_angle = Solar_angle(8, 2006, 6, 15, 9, 53, 49, 120.6333, 39.9,
                                Declination=23.312591474528226)
        time_angle, Declination_angle, son_alpha, Solar_Azimuth = Solar_angle.Calcu()

        time_angle = format(time_angle, '.6f')
        declination_angle = format(Declination_angle, '.6f')
        solar_high_angle = format(son_alpha, '.6f')
        solar_azimuth_angle = format(Solar_Azimuth, '.6f')

        self.Time_AgleWT.setText(str(time_angle))
        self.ChiWei_AgleWT.setText(str(declination_angle))
        self.SunHight_AgleWT.setText(str(solar_high_angle))
        self.SunAzim_AgleWT.setText(str(solar_azimuth_angle))
    # 清空所有可编辑控件
    def ClearKeyValue(self):

        self.Yearwt_LEdit.clear()
        self.Month_LEdit.clear()
        self.Day_LEdit.clear()
        self.PthHigh_LinEdit.clear()
        self.HourComboBox.setCurrentText("0")
        self.MinutesComboBox.setCurrentText("00")
        self.SecondComboBox.setCurrentText("00")

        item = QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 1, item)

        item = QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 2, item)

        item_r = QTableWidgetItem("")
        item_r.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 3, item_r)

        item = QTableWidgetItem("")
        item.setTextAlignment(Qt.AlignCenter)
        self.TabelView.setItem(0, 4, item)

        self.LongF_LEdit.clear()
        self.LongS_LEdit.clear()
        self.LongT_LEdit.clear()
        self.LongRead_LEdit.clear()

        self.LatF_LEdit.clear()
        self.LatS_LEdit.clear()
        self.LatT_LEdit.clear()
        self.LatRead_LEdit.clear()

        self.TimeZoomComboBox.setCurrentText("零时区")

        self.Time_AgleWT.clear()
        self.ChiWei_AgleWT.clear()
        self.SunHight_AgleWT.clear()
        self.SunAzim_AgleWT.clear()


if __name__ == '__main__':
    from sys import argv, exit

    app = QApplication(argv)
    app.setStyleSheet(load_stylesheet_pyqt5())

    demo = Main_Window()
    demo.show()
    exit(app.exec_())